<!--
 * @Author: Dorange.Wang
 * @Date: 2021-03-31 16:04:51
 * @LastEditors: wangming
 * @LastEditTime: 2021-10-20 10:37:37
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ title }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="formData"
      ref="form"
      label-width="120px"
      label-position="left"
    >
      <el-form-item
        label="模态框名称"
        prop="name"
        :rules="[
          { required: true, message: '请输入模态框名称', trigger: 'blur' },
        ]"
      >
        <el-input
          v-model="formData.name"
          style="width: 400px"
          placeholder="请输入模态框名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cloneDeep } from "lodash";
import { newform } from "../module.config";
export default {
  data() {
    return {
      title: "",
      showVisible: false,
      formData: {},
      callback: () => {},
    };
  },
  methods: {
    addForm(title = "新建模态框") {
      this.title = title;
      this.formData = newform();
      return this.showModel();
    },
    editForm(formData, title = "新建模态框") {
      this.title = title;
      this.formData = cloneDeep(formData);
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.form.validate();
        this.showVisible = false;
        this.callback(this.formData);
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped></style>
